Summary

N Active Users Swap Trades Trades_Per User Trading Volume
ALL 7627 0.6 0.5 0.8 0.4
KAR:KSM 3266 0.5 0.5 0.9 0.4
KUSD:KAR 983 0.5 0.5 0.9 0
KAR:LKSM 485 0.7 1 1.5 1.3
KUSD:KSM 465 0.4 0.4 1.1 0.2
LKSM:KSM 437 0.4 0.5 1.1 0.7
KSM:BNC 317 0.1 0.3 1.5 0.4
KUSD:LKSM 195 0.4 0.3 0.6 1.3
KAR:BNC 138 0.1 0.2 1.8 0.2
KUSD:BNC 132 0.3 0.2 0.7 0.3
LKSM:BNC 16 0.9 0.9 1 0.1

Last updated: 2021-12-25 19:05:36

Date range of data: 2021-12-18T00:08:00.964 to 2021-12-25T23:55:00.281.

Source: SubQuery Network

---
title: "Karura Swap Performance"
output:
  flexdashboard::flex_dashboard:
    orientation: rows
    vertical_layout: scroll
    social: menu
    source_code: embed
runtime: shiny
---

```{css custom1, echo=FALSE}
.dataTables_scrollBody {
    max-height: 100% !important;
}
```

```{r global, include=FALSE}

knitr::opts_chunk$set(
  message = FALSE,
  warning = FALSE,
  comment = "#>"
)

library(kableExtra)
library(formattable)
library(lubridate)
library(flexdashboard)
library(DT)

# Helper function to concat
`%+%` <- function(a, b) paste0(a, b)


# helper function to color the bars in the charts (green, yellow, & red)
getColor <- function(status) {
  ifelse(status >= 1, "#34FF33", ifelse(status >= .5, "#FFC300", "#FF5733"))
}

# remotes::install_github("ropensci/ghql") # if package is not already installed
library(jsonlite)
library(data.table)
library("ghql")
x <- GraphqlClient$new()

endpoint <- "https://api.polkawallet.io/acala-subql"
window <- 8 # days to read in (latest day plus 7 day average)

# make a client
cli <- GraphqlClient$new(
  url = endpoint,
  headers = list("Accept-Encoding: gzip, deflate, br",
                 "Content-Type: application/json",
                 "Accept: application/json",
                 "Connection: keep-alive",
                 "DNT: 1",
                 "Origin: https://api.polkawallet.io")
)


mindate <- today(tzone = 'UTC') - window

### create a query class first
qry <- Query$new()
qry$query('dexActions', '{
  query {
    dexActions(filter: {timestamp: {greaterThanOrEqualTo: "' %+% mindate %+% '"}}) {
      nodes {
        timestamp
        id
        nodeId
        accountId
        type
        token0Id
        token1Id
        token0Amount
        token1Amount
        volumeUSD
        extrinsicId
        data
      }
    }
  }
}')
result <- cli$exec(qry$queries$dexActions) %>%
  fromJSON(flatten=TRUE)
res <- as.data.table(result$data$query$dexActions$nodes)

if (substr(max(res$timestamp), 12, 13) < 23) {
  maxdate <- as.Date(max(res$timestamp))-1
} else {
  maxdate <- as.Date(max(res$timestamp))
}

res <- res[timestamp <= maxdate]

res[, data := NULL]
res[, date := as.Date(timestamp)]
setorder(res, timestamp)

# Create pairs
res[, pair := paste0(res$token0Id, ":", res$token1Id)]
res[, exclude := token0Id == token1Id]

# Normalize pairs
res[pair == "KSM:KAR",  pair := "KAR:KSM"]
res[pair == "KSM:LKSM", pair := "LKSM:KSM"]
res[pair == "KSM:KUSD", pair := "KUSD:KSM"]
res[pair == "LKSM:KAR", pair := "KAR:LKSM"]
res[pair == "LKSM:KUSD",pair := "KUSD:LKSM"]
res[pair == "LKSM:BNC", pair := "BNC:LKSM"]
res[pair == "BNC:KAR",  pair := "KAR:BNC"]
res[pair == "BNC:LKSM", pair := "LKSM:BNC"]
res[pair == "BNC:KUSD", pair := "KUSD:BNC"]
res[pair == "KAR:KUSD", pair := "KUSD:KAR"]
res[pair == "BNC:KSM",  pair := "KSM:BNC"]

pairs <- rbind(res[exclude == FALSE & type == 'swap', .N, by = pair], data.table(pair="ALL", N = res[, .N]))[order(N, decreasing = TRUE)] %>% 
  setnames(c("Pair", "Observations"))




# Calculate measures for each pair
user_status   <- list()
trades_status <- list()
tpu_status    <- list()
volume_status <- list()

users_list  <- list()
trades_list <- list()
per_list    <- list()
volume_list <- list()

for (p in pairs$Pair) {
  
  try(rm(u_list, t_list, p_list, v_list), silent = TRUE)
  
  outname <- "~/R_HOME/websites/web_acala/content/swap_" %+% p %+% ".html"
  unlink(outname)
  # Create report for each pair
  rmarkdown::render("~/R_HOME/karura-reports/Swap_template.Rmd",
                  output_file = outname,
                  params = list(pair = p))
  
  # Store the data for the table
  user_status[p]   <- activeUsersStatus
  trades_status[p] <- tradesStatus
  tpu_status[p]    <- avgTradeStatus
  volume_status[p] <- tradeVolumeStatus
  
  users_list[[p]]  <- u_list
  trades_list[[p]] <- t_list
  per_list[[p]]    <- p_list
  volume_list[[p]] <- v_list

}

  d <- list()
  for (x in pairs$Pair) {
    d[x] <- paste0('', 
                     x, 
                     '', collapse = "")
  }

  inline_plot <- data.frame(N = pairs$Observations, 
                            Active = unlist(user_status),
                            Users = "",
                            Swap = unlist(trades_status),
                            Trades = "",
                            Trades_Per = unlist(tpu_status),
                            User = "",
                            Trading = unlist(volume_status),
                            Volume = "")
  row.names(inline_plot) <- unlist(d)

  inline_plot$Active <- cell_spec(inline_plot$Active, color = ifelse(inline_plot$Active < 1, "red", "green"))

  inline_plot$Swap <- cell_spec(inline_plot$Swap, color = ifelse(inline_plot$Swap < 1, "red", "green"))
  
  inline_plot$Trades_Per <- cell_spec(inline_plot$Trades_Per, color = ifelse(inline_plot$Trades_Per < 1, "red", "green"))

  inline_plot$Trading <- cell_spec(inline_plot$Trading, color = ifelse(inline_plot$Trading < 1, "red", "green"))

  p <- inline_plot %>%
    kbl(booktabs = TRUE, escape = FALSE) %>%
    kable_paper(full_width = FALSE) %>%
    column_spec(4, image = spec_plot(users_list, same_lim = FALSE)) %>%
    column_spec(6, image = spec_plot(trades_list, same_lim = FALSE)) %>%
    column_spec(8, image = spec_plot(per_list, same_lim = FALSE)) %>%
    column_spec(10, image = spec_plot(volume_list, same_lim = FALSE))

```

### Summary

```{r plot, result='asis', out.height = 12}
p
```

Last updated: `r Sys.time()`

Date range of data: `r min(res$timestamp)` to `r max(res$timestamp)`.

Source: [SubQuery Network](https://explorer.subquery.network/)